package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.Set;

import models.ExercicioTreino;
import models.Treino;

public class DAOExercicioTreino {

	public Set<ExercicioTreino> select() throws Exception {
		Set<ExercicioTreino> objs = new HashSet<ExercicioTreino>();
		try {

			String query = "SELECT idTreino, idExercicio, repeticao, serie, descanso FROM ExercicioTreino";

			Statement statement = DAOConnection.getConnection().createStatement();

			ResultSet resultSet = statement.executeQuery(query);

			while(resultSet.next()) {
				ExercicioTreino obj = new ExercicioTreino();
				obj.idTreino = resultSet.getInt(1);
				obj.idExercicio = resultSet.getInt(2);
				obj.repeticao = resultSet.getInt(3);
				obj.serie = resultSet.getInt(4);
				obj.descanso = resultSet.getInt(5);
				objs.add(obj);
			}
		} catch(SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
		return objs; 
	}

	public ExercicioTreino select(int idTreino, int idExercicio) throws Exception {
		ExercicioTreino obj = null;
		try	{

			String query = "SELECT idTreino, idExercicio, repeticao, serie, descanso FROM ExercicioTreino WHERE idTreino = ? AND idExercicio = ?";

			PreparedStatement statement = DAOConnection.getConnection().prepareStatement(query);
			statement.setInt(1, idTreino);
			statement.setInt(2, idExercicio);
			
			ResultSet resultSet = statement.executeQuery();

			while(resultSet.next()) {
				obj = new ExercicioTreino();
				obj.idTreino = resultSet.getInt(1);
				obj.idExercicio = resultSet.getInt(2);
				obj.repeticao = resultSet.getInt(3);
				obj.serie = resultSet.getInt(4);
				obj.descanso = resultSet.getInt(5);
			}
		} catch(SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
		return obj; 
	}

	public void insert(ExercicioTreino te) throws Exception {
		try {
			String insert = "INSERT INTO ExercicioTreino(idTreino, idExercicio, repeticao, serie, descanso) VALUES(?, ?, ?, ?, ?)";

			PreparedStatement ps = DAOConnection.getConnection().prepareStatement(insert);
			ps.setInt(1, te.idTreino);
			ps.setInt(2, te.idExercicio);
			ps.setInt(3, te.repeticao);
			ps.setInt(4, te.serie);
			ps.setInt(5, te.descanso);

			ps.executeUpdate();
		} catch(SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
	}

	public void update(ExercicioTreino te) throws Exception {
		try {

			String update = "UPDATE ExercicioTreino SET repeticao = ?, serie = ?, descanso = ? WHERE idTreino = ? AND idExercicio = ?";

			PreparedStatement ps = DAOConnection.getConnection().prepareStatement(update);
			ps.setInt(1, te.repeticao);
			ps.setInt(2, te.serie);
			ps.setInt(3, te.descanso);
			ps.setInt(4, te.idTreino);
			ps.setInt(5, te.idExercicio);

			ps.executeUpdate();
		} catch(SQLException e) {
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}

	}

	public void delete(ExercicioTreino te) throws Exception {
		try {

			String delete = "DELETE FROM ExercicioTreino WHERE idTreino = ? AND idExercicio = ?";

			PreparedStatement ps = DAOConnection.getConnection().prepareStatement(delete);
			ps.setInt(1, te.idTreino);
			ps.setInt(2, te.idExercicio);

			ps.executeUpdate();
		} catch(SQLException e) {
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
	}
	
	public void delete(Treino t) throws Exception {
		try {

			String delete = "DELETE FROM ExercicioTreino WHERE idTreino = ?";

			PreparedStatement ps = DAOConnection.getConnection().prepareStatement(delete);
			ps.setInt(1, t.idTreino);

			ps.executeUpdate();
		} catch(SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
	}

}